<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/21
 * Time: 上午11:15
 */

class AccAuth extends Service {

    /**
     * 获得当前登录的个人信息
     * @param bool $key
     * @return mixed
     */
    public static function me($key=false){
        $user = PubAuth::getUser();
        if($key){
            return $user[$key];
        }else{
            return $user;
        }
    }

    /**
     * 获得我的App
     * @param bool $userType    若不为False，则按此用户类型筛选App
     * @return mixed
     */
    public static function getMyApp($userType = false){
        $app = self::me('App');

        if(!is_array($userType)){
            $userType = array(strtoupper($userType));
        }else{
            foreach($userType as &$v){
                $v = strtoupper($v);
            }
            unset($v);
        }

        $result = array();
        foreach($app as $k=>$v){
            if(in_array(strtoupper($v['UserType']), $userType))
                $result[$k] = $v;
        }

        return $result;
    }
    
    /**
     * 获得当前角色在某个APP下的菜单
     */
    public static function getRoleMenu($userId){
    	$modMenu = new ModMenu();
    
    	$menuList = array();
    	foreach($modMenu->select() as $v){
    		$menuList[$v['MenuID']] = $v;
    	}
    
    	$modUser = new ModUser();
    	$user = $modUser->getUserById($userId);
    	if(empty($user)) return array();
    	$isAdmin = self::isAdmin($user['IsAdmin']);
    
    	if($isAdmin){
    		// 若用户为这个系统的超级管理员，则获得这个菜单的所有信息
    		$result = $menuList;
    	}else{
    		$result = array();
    		// 否则查询这个用户所有角色的菜单信息
    		$modUserRoleMap = new ModUserRoleMap();
            $role = $modUserRoleMap->findByAttributes(array(
        		'UserID' => $user['UserID'],
            ))->attributes;
    
    		$myMenu = ModMenu::getRoleMenu($role['RoleID']);
    		foreach($myMenu as $v){
    			if(isset($menuList[$v])){
    				$result[$v] = $menuList[$v];
    			}
    		}
    
    		$pubSort = new PubMultiSort();
    		$pubSort->setSort('SortNum', 'asc');
    		$result = $pubSort->getResult($result);
    	}
    	return $result;
    }
    

    /**
     * 获得当前用户在某个APP下的菜单
     */
    public static function getMyMenu($userId){
        $modMenu = new ModMenu();

        $menuList = array();
        foreach($modMenu->select() as $v){
            $menuList[$v['MenuID']] = $v;
        }

        $modUser = new ModUser();
        $user = $modUser->getUserById($userId);
        if(empty($user)) return array();
        $isAdmin = self::isAdmin($user['IsAdmin']);

        if($isAdmin){
            // 若用户为这个系统的超级管理员，则获得这个菜单的所有信息
            $result = $menuList;
        }else{
            $result = array();
            // 否则查询这个用户所有角色的菜单信息

            $myMenu = ModMenu::getUserMenu($userId);
            foreach($myMenu as $v){
                if(isset($menuList[$v])){
                    $result[$v] = $menuList[$v];
                }
            }

            $pubSort = new PubMultiSort();
            $pubSort->setSort('SortNum', 'asc');
            $result = $pubSort->getResult($result);
        }
        return $result;
    }

    public static function getMyGame($userId=null){
        $result = array('Details' => array(), 'Platform' => array(), 'Game' => array());
        if(empty($userId)) return $result;

        $modUser = new ModUser();
        $user = $modUser->getUserById($userId);
        if(empty($user)) return $result;

        $modPlatformGameMap = new ModPlatformGameMap();
        $gameList = $gamePlatformMap = array();
        foreach($modPlatformGameMap->select() as $v){
            $key = "{$v['PlatformID']}-{$v['GameID']}";
            $gameList[$key] = $v;
            $gamePlatformMap[$v['GameID']][] = $v['PlatformSrc'];
        }

        $tmp = $gameList;

        $result['Details'] = $tmp;

        foreach($tmp as $v){
            $result['Platform'][$v['PlatformSrc']] = array(
                'PlatformID'        => $v['PlatformID'],
                'PlatformSrc'       => $v['PlatformSrc'],
                'PlatformName'      => $v['PlatformName'],
                'SortNum'           => $v['PlatformSort'],
            );
            $result['Game'][$v['GameSrc']] = array(
                'GameID'        => $v['GameID'],
                'GameSrc'       => $v['GameSrc'],
                'GameName'      => $v['GameName'],
                'GameRemark'    => $v['GameRemark'],
                'GameIcon'      => $v['GameIcon'],
                'SortNum'       => $v['GameSort'],
                'Platform'      => $gamePlatformMap[$v['GameID']]
            );
        }

        $pubSort = new PubMultiSort('SortNum', 'asc');
        if(!empty($result['Platform'])){
            $result['Platform'] = $pubSort->getResult($result['Platform']);
        }
        if(!empty($result['Game'])){
            $result['Game'] = $pubSort->getResult($result['Game']);
        }

        return $result;
    }

    /**
     * 判断是否UC管理员
     * @return bool
     */
    public static function isAdmin($accAdmin=null){
        if(empty($accAdmin)){
            $accAdmin = self::me('IsAdmin');
        }

        return ($accAdmin == '1');
    }

    /**
     * 判断是否有所有游戏
     * @return boll
     */
    public static function hasAllGame($col=null){
        return $col == '1';
    }

    /**
     * 获得用户在所有系统下面的角色
     */
    public static function getUserRole(){
        $app = self::me('App');

        $result = array();
        foreach($app as $v){
            $result[$v['UserType']] = $v['UserType'];
        }

        return array_values($result);
    }
} 